import os

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time


class TestCase:
    def __init__(self):
        self.driver = webdriver.Chrome()
        url = 'file:///' + os.path.abspath('html1') + '/注册实例.html'
        self.driver.get(url)
        self.driver.maximize_window()

    def register(self, user, passwd, tel, email):
        self.driver.find_element(By.ID, 'user').send_keys(user)
        self.driver.find_element(By.ID, 'password').send_keys(passwd)
        self.driver.find_element(By.ID, 'tel').send_keys(tel)
        self.driver.find_element(By.ID, 'email').send_keys(email)
        self.driver.find_element(By.XPATH, '//*[@id="zc"]/fieldset/button').click()

    def register_a(self, user, passwd, tel, email):
        # iframe标签的webelement对象切换到A的frame
        register_A = self.driver.find_element(By.ID, 'idframe1')
        self.driver.switch_to.frame(register_A)

        # 滚到鼠标到下面
        time.sleep(2)
        js1 = 'window.scrollTo(0, document.body.scrollHeight)'
        self.driver.execute_script(js1)
        # 完成页面A注册
        self.driver.find_element(By.ID, 'userA').send_keys(user)
        self.driver.find_element(By.ID, 'passwordA').send_keys(passwd)
        self.driver.find_element(By.ID, 'telA').send_keys(tel)
        self.driver.find_element(By.ID, 'emailA').send_keys(email)
        time.sleep(3)
        self.driver.find_element(By.XPATH, '//*[@id="zc"]/fieldset/button').click()

        # 返回主页面
        self.driver.switch_to.parent_frame()

    def register_b(self, user, passwd, tel, email):
        # 使用iframe标签的name值切换到B的frame
        self.driver.switch_to.frame('myframe2')
        # 滚到鼠标到下面
        time.sleep(2)
        js1 = 'window.scrollTo(0, document.body.scrollHeight)'
        self.driver.execute_script(js1)
        # 完成页面A注册
        self.driver.find_element(By.ID, 'userB').send_keys(user)
        self.driver.find_element(By.ID, 'passwordB').send_keys(passwd)
        self.driver.find_element(By.ID, 'telB').send_keys(tel)
        self.driver.find_element(By.ID, 'emailB').send_keys(email)
        time.sleep(3)
        self.driver.find_element(By.XPATH, '//*[@id="zc"]/fieldset/button').click()

        # 返回主页面
        self.driver.switch_to.parent_frame()

    def select_frame(self):
        # 主页面
        self.driver.switch_to.parent_frame()

        # 下拉框选择地址
        s = self.driver.find_element(By.ID, 'select')
        select = Select(s)
        time.sleep(2)
        select.select_by_value('cq')

        # 弹框处理
        register_A = self.driver.find_element(By.ID, 'idframe1')
        self.driver.switch_to.frame(register_A)

    def select_frame1(self):
        # A页面
        self.driver.switch_to.frame('myframe1')

        # 下拉框选择
        s1 = self.driver.find_element(By.ID, 'selectA')
        select = Select(s1)
        select.select_by_index(1)

        # 弹框处理
        btn = self.driver.find_element(By.ID, 'alerta')
        btn.click()
        time.sleep(2)
        alerta = self.driver.switch_to.alert
        alerta.accept()

        # 返回主页面
        self.driver.switch_to.parent_frame()

    def select_frame2(self):
        # B页面
        self.driver.switch_to.frame('myframe2')

        # 下拉框选择
        s2 = self.driver.find_element(By.ID, 'selectB')
        select = Select(s2)
        select.select_by_visible_text('广州B')

        # 弹框处理
        btn = self.driver.find_element(By.ID, 'alertB')
        btn.click()
        time.sleep(2)
        alertB = self.driver.switch_to.alert
        alertB.accept()

        # 返回主页面
        self.driver.switch_to.parent_frame()

    def quit(self):
        time.sleep(3)
        self.driver.quit()


if __name__ == '__main__':
    case = TestCase()
    # case.register('12345','1234','1234','123@qq.com')
    # case.register_a('12345','1234','1234','123@qq.com')
    # case.register_b('12345','1234','1234','123@qq.com')
    # case.select_frame()
    # case.select_frame1()
    # case.select_frame2()
    # case.quit()
